package cn.com.open.itembank.controller;

/**
 * Created by lucky on 2017/7/31.
 */

import cn.com.open.itembank.dto.ReturnModel;
import cn.com.open.itembank.interceptor.Log;
import cn.com.open.itembank.common.GlobalConst;
import cn.com.open.itembank.dto.Category;
import cn.com.open.itembank.entity.QuestionCategory;
import cn.com.open.itembank.service.QuestionCategoryRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 试题应用类别控制器
 */
@RestController
@RequestMapping("/questioncategory")
public class QuestionCategoryController extends BaseController {
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(QuestionCategoryController.class);

    @Autowired
    private QuestionCategoryRemoteService service;

    //region 新增相关（Create：理论方法 HttpPost）

    //region 新增试题应用类别

    /**
     * 新增试题应用类别
     *
     * @param request  the request
     * @param response the response
     */
    @Log(optType = "新增试题应用类别", optDescription = "新增试题应用类别，参数：%s", keyName = "model")
    @RequestMapping(method = RequestMethod.POST)
    public void createQuestionCategory(HttpServletRequest request, HttpServletResponse response,
                                       @RequestBody Category model) {

        //调用服务层方法
        ReturnModel result = service.insert(model);

        if (result.getCode() == GlobalConst.DATA_EXIST) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
        } else {
            responseJson(request, response, result.getData(), result.getMessage(), result.getCode());
        }
    }

    //endregion

    //endregion

    //region 删除相关（Delete：理论方法 HttpDelete）

    //region 删除试题应用类别

    /**
     * 删除试题应用类别
     *
     * @param request  the request
     * @param response the response
     * @param id       试题类别id
     */
    @Log(optType = "删除试题应用类别", optDescription = "删除试题应用类别，ID：%s", keyName = "id")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteQuestionCategory(HttpServletRequest request, HttpServletResponse response,
                                       @PathVariable(value = "id") String id) {

        int result = service.delete(id);

        if (result == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (result == GlobalConst.DATA_EXIST) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
        } else {
            responseJson(request, response, "", "", result);
        }
    }

    //endregion

    //endregion

    //region 更新相关（Update：理论方法 HttpPut）

    //region 修改试题应用类别

    /**
     * 修改试题应用类别
     *
     * @param request              the request
     * @param response             the response
     * @param id                   试题类别id
     * @param questionCategoryName 试题类别名称
     */
    @Log(optType = "修改试题应用类别", optDescription = "修改试题应用类别，ID：%s", keyName = "id")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public void updateQuestionCategory(HttpServletRequest request, HttpServletResponse response,
                                       @PathVariable(value = "id") String id,
                                       @RequestParam("questionCategoryName") String questionCategoryName) {

        int result = service.update(id, questionCategoryName);

        if (result == GlobalConst.COURSE_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (result == GlobalConst.DATA_EXIST) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
        } else {
            responseJson(request, response, "", "", result);
        }
    }

    //endregion

    //region 修改试题应用类别状态

    /**
     * 修改试题应用类别状态
     *
     * @param request  the request
     * @param response the response
     * @param id       试题类别id
     */
    @Log(optType = "修改试题应用类别状态", optDescription = "修改试题应用类别状态，ID：%s", keyName = "id")
    @RequestMapping(value = "/{id}/status", method = RequestMethod.PUT)
    public void updateQuestionCategoryStatus(HttpServletRequest request, HttpServletResponse response,
                                             @PathVariable(value = "id") String id) {

        int result = service.changeStatus(id);

        if (result == GlobalConst.COURSE_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, "", "", result);
        }
    }

    //endregion

    //endregion

    //region 查询相关（Retrieve：理论方法 HttpGet）

    //region 获取试题应用类别信息

    /**
     * 获取试题应用类别信息
     *
     * @param request  the request
     * @param response the response
     * @param id       试题类别id
     */
    @Log(optType = "获取试题应用类别信息", optDescription = "获取试题应用类别信息，ID：%s", keyName = "id")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public void getQuestionCategoryById(HttpServletRequest request, HttpServletResponse response,
                                        @PathVariable(value = "id") String id) {

        QuestionCategory model = service.getQuestionCategoryInfo(id);
        if (model != null) {
            responseJson(request, response, model, "");
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    //endregion

    //region 获取试题应用类别列表

    /**
     * 获取试题应用类别列表
     *
     * @param request  the request
     * @param response the response
     */
    @Log(optType = "获取试题应用类别列表", optDescription = "获取试题应用类别列表，ID：%s", keyName = "name")
    @RequestMapping(method = RequestMethod.GET)
    public void getQuestionCategoryList(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(value = "name", defaultValue = "") String name) {

        List<QuestionCategory> model = service.getList(name);
        if (model != null) {
            responseJson(request, response, model, "");
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    //endregion

    //region 获取试题应用类别列表(启用的)

    /**
     * 获取试题应用类别列表（启用的）
     *
     * @param request  the request
     * @param response the response
     */
    @Log(optType = "获取试题应用类别列表（启用的）", optDescription = "获取试题应用类别列表（启用的），名称：%s", keyName = "name")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public void getEnabledQuestionCategoryList(HttpServletRequest request, HttpServletResponse response,
                                               @RequestParam(value = "name", defaultValue = "") String name) {

        List<QuestionCategory> model = service.getEnabledList(name);
        if (model != null) {
            responseJson(request, response, model, "");
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    //endregion

    //endregion
}
